package de.bk.timecalc.midlet.statemachine;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

public abstract class AbstractCommandListener implements CommandListener
{
  private AbstractGuiState state;

  AbstractCommandListener(AbstractGuiState state)
  {
    this.state = state;
  }

  final AbstractGuiState getState()
  {
    return this.state;
  }

  final void setCurrent(Displayable displayable)
  {
    this.getState().getDisplay().setCurrent(displayable);
  }

  /**
   * {@inheritDoc}
   * 
   * @see CommandListener#commandAction(Command, Displayable)
   */
  public void commandAction(Command command, Displayable displayable)
  {
    if (this.getState().followTransition(command))
    {
      return;
    }
    else
    {
      throw new RuntimeException("Unknown command or no transition defined: ["
          + command.getLabel() + "]");
    }
  }
}
